#include <iostream>
#include <string>
using namespace std;
int t, n;
int main(){
	cin >> t;
	while(t--){
		string s;
		cin >> n;
		cin >> s;
		if ((s.find('1') != -1) && (s.find('2') != -1) && (s.find('3') != -1)){
			if (s.find("123") != -1 || s.find("132") != -1 || s.find("213") != -1 || s.find("231") != -1 || s.find("312") != -1 || s.find("321") != -1){
				cout << 0 << endl;
			}
			else if (s.find("12") != -1 || s.find("13") != -1 || s.find("21") != -1 || s.find("23") != -1 || s.find("31") != -1 || s.find("32") != -1){
				cout << 1 << endl;
			}
			else {
				cout << 2 << endl;
			}
		}
		else {
			cout << -1 << endl;
		}
	}

    return 0;
}


